<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="My Personal Website For Blog">
  <meta name="author" content="黄宇辉">
  <meta name="keywords" content="learning note,hexo blog">
  <title>Java设计模式之Builder Pattern - 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/vs2015.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>ANONYMOUS</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2019-09-17 22:00">
      September 17, 2019 pm
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      29
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h2 id="学习笔记-Java设计模式之Builder-Pattern"><a href="#学习笔记-Java设计模式之Builder-Pattern" class="headerlink" title="学习笔记 : Java设计模式之Builder Pattern"></a>学习笔记 : Java设计模式之Builder Pattern</h2><h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><p><em>缔造者设计模式 : 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示( Builder Pattern : Separate the construction of complex object from its representation so that the same construction process can create different representations )</em></p>
<ul>
<li>扩 : 建造者模式是一种对象创建型模式,它将客户端与包含多个部件的复杂对象的创建过程分离,客户端无需知道复杂对象的内部组成部分与装配方式,只需要知道所需建造者的类型即可.</li>
</ul>
<h3 id="建造者设计模式中的角色"><a href="#建造者设计模式中的角色" class="headerlink" title="建造者设计模式中的角色"></a>建造者设计模式中的角色</h3><ol>
<li><em><code>Builder( 抽象建造者 )</code> : 它为创建一个产品的各个部件指定抽象接口,在该接口中一般声明两类方法,一类方法是 buildPartX(),其用于创建复杂对象的各个部件. 另一类方法是 getResult(),其用于返回复杂对象. Builder 即可是抽象类,也可以是接口.</em></li>
<li><em><code>ConcreteBuilder( 具体建造者 )</code> : 它实现了 Builder 接口,实现了各个部件的具体构造和装配方法,定义并明确所创建的复杂对象,还可以提供一个用于返回创建好的复杂产品的方法( 该方法可以由抽象构建者实现 ).</em></li>
<li><em><code>Product( 产品 )</code> : 它是被创建的复杂对象,包含多个组成部件,具体建造者创建该产品的内部表示并定义它的装配过程.</em></li>
<li><em><code>Director( 指挥者 )</code> : 指挥者又称为导演类,它负责安排复杂对象的建造次序,指挥者与抽象构建者之间存在关联关系,可以在其 construct() 建造方法中调用建造者对象的部件构造与装配方法,完成复杂对象的建造.</em></li>
</ol>
<h3 id="示例程序"><a href="#示例程序" class="headerlink" title="示例程序"></a>示例程序</h3><p><em>下面是一个使用了建造者设计模式的简单示例程序,其概述为 : 无论是何种游戏角色,都需要逐步创建其组成部分,再将各组成部分配装成一个完整的游戏角色.</em></p>
<ol>
<li><p><em>Actor类 : 游戏角色类, 充当复杂产品对象</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 游戏角色类, 充当复杂产品对象
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:30 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Actor</span> </span>&#123;
    <span class="hljs-keyword">private</span> String type; <span class="hljs-comment">//角色类型</span>
    <span class="hljs-keyword">private</span> String sex; <span class="hljs-comment">//性别</span>
    <span class="hljs-keyword">private</span> String face; <span class="hljs-comment">//脸型</span>
    <span class="hljs-keyword">private</span> String costume; <span class="hljs-comment">//服装</span>

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getType</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> type;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setType</span><span class="hljs-params">(String type)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.type = type;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getSex</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> sex;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setSex</span><span class="hljs-params">(String sex)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.sex = sex;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getFace</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> face;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setFace</span><span class="hljs-params">(String face)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.face = face;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getCostume</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> costume;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setCostume</span><span class="hljs-params">(String costume)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.costume = costume;
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Actor&#123;"</span> +
                <span class="hljs-string">"type='"</span> + type + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">", sex='"</span> + sex + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">", face='"</span> + face + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">", costume='"</span> + costume + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">'&#125;'</span>;
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>ActorBuilder抽象类 : 游戏角色建造者, 充当抽象建造者</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 游戏角色建造者, 充当抽象建造者
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:39 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ActorBuilder</span> </span>&#123;
    <span class="hljs-keyword">protected</span> Actor actor = <span class="hljs-keyword">new</span> Actor();

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildType</span><span class="hljs-params">()</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildSex</span><span class="hljs-params">()</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildFace</span><span class="hljs-params">()</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildCostume</span><span class="hljs-params">()</span></span>;

    <span class="hljs-comment">//工厂方法,返回一个完整的游戏角色对象</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Actor <span class="hljs-title">createActor</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> actor;
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>HeroBuilder类 : 英雄角色建造者, 充当具体建造者</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 英雄角色建造者, 充当具体建造者
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:42 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroBuilder</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ActorBuilder</span> </span>&#123;
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildType</span><span class="hljs-params">()</span> </span>&#123;
        actor.setType(<span class="hljs-string">"英雄"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildSex</span><span class="hljs-params">()</span> </span>&#123;
        actor.setSex(<span class="hljs-string">"男"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildFace</span><span class="hljs-params">()</span> </span>&#123;
        actor.setFace(<span class="hljs-string">"英俊"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildCostume</span><span class="hljs-params">()</span> </span>&#123;
        actor.setCostume(<span class="hljs-string">"盔甲"</span>);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>AngelBuilder类 : 天使角色建造者, 充当具体建造类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 天使角色建造者, 充当具体建造类
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:44 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AngelBuilder</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ActorBuilder</span> </span>&#123;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildType</span><span class="hljs-params">()</span> </span>&#123;
        actor.setType(<span class="hljs-string">"天使"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildSex</span><span class="hljs-params">()</span> </span>&#123;
        actor.setSex(<span class="hljs-string">"女"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildFace</span><span class="hljs-params">()</span> </span>&#123;
        actor.setFace(<span class="hljs-string">"漂亮"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildCostume</span><span class="hljs-params">()</span> </span>&#123;
        actor.setCostume(<span class="hljs-string">"裙子"</span>);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>ActorController类 : 角色控制器, 充当指挥者</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 角色控制器, 充当指挥者
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:46 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ActorController</span> </span>&#123;

    <span class="hljs-comment">//逐步构建复杂产品对象</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Actor <span class="hljs-title">construct</span><span class="hljs-params">(ActorBuilder actorBuilder)</span> </span>&#123;
        Actor actor;
        actorBuilder.buildType();
        actorBuilder.buildSex();
        actorBuilder.buildFace();
        actorBuilder.buildCostume();
        actor = actorBuilder.createActor();
        <span class="hljs-keyword">return</span> actor;
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>Client类 : 客户端测试类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 客户端测试类
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:49 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> </span>&#123;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;
        ActorController actorController = <span class="hljs-keyword">new</span> ActorController();

        ActorBuilder zero = <span class="hljs-keyword">new</span> HeroBuilder();
        Actor actor_zero = actorController.construct(zero);
        System.out.println(actor_zero);

        System.out.println(<span class="hljs-string">"------------------------------"</span>);

        ActorBuilder angel = <span class="hljs-keyword">new</span> AngelBuilder();
        Actor actor_angel = actorController.construct(angel);
        System.out.println(actor_angel);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>示例程序的运行结果如下所示 :</em></p>
<div class="hljs"><pre><code class="hljs routeros">Actor&#123;<span class="hljs-attribute">type</span>=<span class="hljs-string">'英雄'</span>, <span class="hljs-attribute">sex</span>=<span class="hljs-string">'男'</span>, <span class="hljs-attribute">face</span>=<span class="hljs-string">'英俊'</span>, <span class="hljs-attribute">costume</span>=<span class="hljs-string">'盔甲'</span>&#125;
------------------------------
Actor&#123;<span class="hljs-attribute">type</span>=<span class="hljs-string">'天使'</span>, <span class="hljs-attribute">sex</span>=<span class="hljs-string">'女'</span>, <span class="hljs-attribute">face</span>=<span class="hljs-string">'漂亮'</span>, <span class="hljs-attribute">costume</span>=<span class="hljs-string">'裙子'</span>&#125;</code></pre></div>

</li>
</ol>
<h4 id="示例程序的类图"><a href="#示例程序的类图" class="headerlink" title="示例程序的类图"></a>示例程序的类图</h4><p><img src="/2019/09/17/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BBuilder-Pattern/Builder-Pattern-ClassDiagram1.png" srcset="/img/loading.gif" alt></p>
<h3 id="示例程序-简化版"><a href="#示例程序-简化版" class="headerlink" title="示例程序-简化版"></a>示例程序-简化版</h3><p><em>为了简化系统结构,可以将 Director 和抽象建造者 Builder 进行合并,在 Builder 中提供逐步构建复杂产品对象的 construct() 方法. 由于 Builder 类通常是抽象类,因此可以将 construct() 方法定义为静态方法,以便客户端能够直接调用. 除此之外还有一种更简单的处理方法,既将 construct() 方法中的参数去掉,直接在construct()方法中调用 buildPartX() 方法.( 以下示例程序是基于上述示例程序进行简化的 )</em></p>
<ol>
<li><p><em>Actor类 : 游戏角色类, 充当复杂产品对象</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_2;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 游戏角色类, 充当复杂产品对象
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:30 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Actor</span> </span>&#123;
    <span class="hljs-keyword">private</span> String type; <span class="hljs-comment">//角色类型</span>
    <span class="hljs-keyword">private</span> String sex; <span class="hljs-comment">//性别</span>
    <span class="hljs-keyword">private</span> String face; <span class="hljs-comment">//脸型</span>
    <span class="hljs-keyword">private</span> String costume; <span class="hljs-comment">//服装</span>

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getType</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> type;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setType</span><span class="hljs-params">(String type)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.type = type;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getSex</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> sex;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setSex</span><span class="hljs-params">(String sex)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.sex = sex;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getFace</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> face;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setFace</span><span class="hljs-params">(String face)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.face = face;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getCostume</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> costume;
    &#125;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setCostume</span><span class="hljs-params">(String costume)</span> </span>&#123;
        <span class="hljs-keyword">this</span>.costume = costume;
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Actor&#123;"</span> +
                <span class="hljs-string">"type='"</span> + type + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">", sex='"</span> + sex + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">", face='"</span> + face + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">", costume='"</span> + costume + <span class="hljs-string">'\''</span> +
                <span class="hljs-string">'&#125;'</span>;
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>ActorBuilder抽象类 : 游戏角色建造者, 充当抽象建造者(省略Director并简化construct方法)</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_2;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 游戏角色建造者, 充当抽象建造者(省略Director并简化construct方法)
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:39 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ActorBuilder</span> </span>&#123;
    <span class="hljs-keyword">protected</span> Actor actor = <span class="hljs-keyword">new</span> Actor();

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildType</span><span class="hljs-params">()</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildSex</span><span class="hljs-params">()</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildFace</span><span class="hljs-params">()</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildCostume</span><span class="hljs-params">()</span></span>;

    <span class="hljs-comment">//工厂方法,返回一个完整的游戏角色对象</span>
    <span class="hljs-comment">//public Actor createActor() &#123;</span>
    <span class="hljs-comment">//    return actor;</span>
    <span class="hljs-comment">//&#125;</span>

    <span class="hljs-comment">//逐步构建复杂产品对象</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Actor <span class="hljs-title">construct</span><span class="hljs-params">()</span> </span>&#123;
        <span class="hljs-keyword">this</span>.buildType();
        <span class="hljs-keyword">this</span>.buildSex();
        <span class="hljs-keyword">this</span>.buildFace();
        <span class="hljs-keyword">this</span>.buildCostume();
        <span class="hljs-keyword">return</span> actor;
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>HeroBuilder类 : 英雄角色建造者, 充当具体建造者</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_2;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 英雄角色建造者, 充当具体建造者
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:42 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroBuilder</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ActorBuilder</span> </span>&#123;
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildType</span><span class="hljs-params">()</span> </span>&#123;
        actor.setType(<span class="hljs-string">"英雄"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildSex</span><span class="hljs-params">()</span> </span>&#123;
        actor.setSex(<span class="hljs-string">"男"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildFace</span><span class="hljs-params">()</span> </span>&#123;
        actor.setFace(<span class="hljs-string">"英俊"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildCostume</span><span class="hljs-params">()</span> </span>&#123;
        actor.setCostume(<span class="hljs-string">"盔甲"</span>);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>AngelBuilder类 : 天使角色建造者, 充当具体建造类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_2;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 天使角色建造者, 充当具体建造类
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:44 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AngelBuilder</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ActorBuilder</span> </span>&#123;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildType</span><span class="hljs-params">()</span> </span>&#123;
        actor.setType(<span class="hljs-string">"天使"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildSex</span><span class="hljs-params">()</span> </span>&#123;
        actor.setSex(<span class="hljs-string">"女"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildFace</span><span class="hljs-params">()</span> </span>&#123;
        actor.setFace(<span class="hljs-string">"漂亮"</span>);
    &#125;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">buildCostume</span><span class="hljs-params">()</span> </span>&#123;
        actor.setCostume(<span class="hljs-string">"裙子"</span>);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>Client类 : 客户端测试类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.builder_pattern.builder_pattern_2;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@project</span>: design-patterns
 * <span class="hljs-doctag">@description</span>: 客户端测试类
 * <span class="hljs-doctag">@author</span>: 黄宇辉
 * <span class="hljs-doctag">@date</span>: 9/19/2019-1:49 PM
 * <span class="hljs-doctag">@version</span>: 1.0
 * <span class="hljs-doctag">@website</span>: https://yubuntu0109.github.io/
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> </span>&#123;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;
        Actor zero = <span class="hljs-keyword">new</span> HeroBuilder().construct();
        System.out.println(zero);

        System.out.println(<span class="hljs-string">"------------------------------"</span>);

        Actor angel = <span class="hljs-keyword">new</span> AngelBuilder().construct();
        System.out.println(angel);
    &#125;
&#125;</code></pre></div>
</li>
<li><p><em>示例程序的运行结果如下所示 :</em></p>
<div class="hljs"><pre><code class="hljs routeros">Actor&#123;<span class="hljs-attribute">type</span>=<span class="hljs-string">'英雄'</span>, <span class="hljs-attribute">sex</span>=<span class="hljs-string">'男'</span>, <span class="hljs-attribute">face</span>=<span class="hljs-string">'英俊'</span>, <span class="hljs-attribute">costume</span>=<span class="hljs-string">'盔甲'</span>&#125;
------------------------------
Actor&#123;<span class="hljs-attribute">type</span>=<span class="hljs-string">'天使'</span>, <span class="hljs-attribute">sex</span>=<span class="hljs-string">'女'</span>, <span class="hljs-attribute">face</span>=<span class="hljs-string">'漂亮'</span>, <span class="hljs-attribute">costume</span>=<span class="hljs-string">'裙子'</span>&#125;</code></pre></div>

</li>
</ol>
<h4 id="示例程序的类图-1"><a href="#示例程序的类图-1" class="headerlink" title="示例程序的类图"></a>示例程序的类图</h4><p><img src="/2019/09/17/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BBuilder-Pattern/Builder-Pattern-ClassDiagram2.png" srcset="/img/loading.gif" alt></p>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/design-and-pattern/">design and pattern</a>
                    
                      <a class="hover-with-bg" href="/tags/java/">java</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2019/09/20/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BCommand-Pattern/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java设计模式之Command Pattern</span>
                        <span class="visible-mobile">Previous</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2019/09/12/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BFacade-Pattern/">
                        <span class="hidden-mobile">Java设计模式之Facade Pattern</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Java设计模式之Builder Pattern&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  














</body>
</html>
